﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SMP_QuanLySV.BLL;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_BangDiem : UserControl
    {
        Form parent;
        int lanNhapDiem;
        BLLBangDiem BLL = new BLLBangDiem();
        BLLMonHoc BLLMon = new BLLMonHoc();
        BangDiem selectedInstance = null;
        string selectedID;
        string selectedSVID;

        string trangThai = "";

        public UC_BangDiem(Form parent, int lanNhap)
        {
            InitializeComponent();
            this.parent = parent;

            //set label tieu de lan nhap
            this.lanNhapDiem = lanNhap;
            lblTitleNhapDiem.Text += lanNhapDiem.ToString();
            lblDiem.Text += lanNhapDiem.ToString();

            //fill ten mon hoc vao combobox
            cbbListMonHoc.DataSource = BLLMon.returnListMonHoc();
            cbbListMonHoc.DisplayMember = "TenMonHoc";
            cbbListMonHoc.ValueMember = "MonHocID";
            cbbListMonHoc.SelectedIndex = 0;
        }

        private void UC_BangDiem_Load(object sender, EventArgs e)
        {
            dataGridViewX1.Enabled = false;
            disableLeftPanel(false);
            disableUpperTxt(true);
            btnBack.Enabled = false;
            
            btnInsert.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnSave.Enabled = false;
            btnHuy.Enabled = false;
            chkKhoaNhap.Enabled = false;
        }


        private void btnBatDau_Click(object sender, EventArgs e)
        {
            int hocky = 0;
            if (int.TryParse(txtHK.Text, out hocky) == false || txtNamHoc.Text == "" || txtNamHoc.Text.Contains("-") == false)
            {
                MessageBox.Show("Bạn phải nhập đúng năm học và học kỳ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string IDMon = cbbListMonHoc.SelectedValue.ToString();
                disableLeftPanel(true);
                btnBatDau.Enabled = false;
                // enable cac controls
                btnBack.Enabled = true;
                if (lanNhapDiem == 1) btnInsert.Enabled = true;
                else btnInsert.Enabled = false;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnSave.Enabled = false;
                btnHuy.Enabled = false;
                chkKhoaNhap.Enabled = true;

                dataGridViewX1.DataSource = BLL.returnListDiemByClass(IDMon, txtNamHoc.Text, txtHK.Text);
                HideContent();
                if (dataGridViewX1.DataSource != null)
                {
                    disableUpperTxt(true);
                    dataGridViewX1.Enabled = true;
                    selectedInstance = BLL.retrieveByIndex(0);
                    updateTextboxes(selectedInstance);
                    //btnInsert.Enabled = false;
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            disableLeftPanel(false);
            btnBatDau.Enabled = true;
            dataGridViewX1.DataSource = null;
            clearTextboxes();
            dataGridViewX1.Enabled = false;
            disableLeftPanel(false);
            disableUpperTxt(true);
            btnBack.Enabled = false;
            btnInsert.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnSave.Enabled = false;
            btnHuy.Enabled = false;
            chkKhoaNhap.Enabled = false;
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
            selectedID = dataGridViewX1.Rows[selectedIndex].Cells[0].Value.ToString();
            selectedSVID = dataGridViewX1.Rows[selectedIndex].Cells[1].Value.ToString();
            //selectedSVID = dataGridViewX1.SelectedRows[0].Cells[1].Value.ToString();

            selectedInstance = BLL.retrieveByIDAndSVID(selectedID, selectedSVID);
            updateTextboxes(selectedInstance);
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            disableUpperTxt(false);
            setBtnEnabled2(true);
            setBtnEnabled1(false);

            dataGridViewX1.Enabled = false;
            trangThai = "dang_them";
            chkKhoaNhap.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            disableUpperTxt(false);
            txtMSSV.ReadOnly = true;

            // set trang thai ve che do sua
            trangThai = "dang_sua";
            setBtnEnabled1(false);
            setBtnEnabled2(true);
            dataGridViewX1.Enabled = false;
            chkKhoaNhap.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // kiem tra tinh hop le cua du lieu nhap vao
            BangDiem bdiem = new BangDiem();
            string IDMon = cbbListMonHoc.SelectedValue.ToString();
            bdiem.ID = txtMSSV.Text + IDMon.TrimEnd();
            bdiem.MonHocID = IDMon.TrimEnd();
            bdiem.SVID = txtMSSV.Text;
            bdiem.HocKy = txtHK.Text;
            bdiem.NamHoc = txtNamHoc.Text;
            decimal diem;

            if (Decimal.TryParse(txtDiem.Text, out diem) == false || diem < 0 || diem > 10)
            {
                MessageBox.Show("Bạn phải nhập đúng điểm", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (BLL.kiemTra(bdiem))
                {
                    // update csdl va view
                    if (trangThai == "dang_sua")
                    {
                        int selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
                        selectedID = dataGridViewX1.Rows[selectedIndex].Cells[0].Value.ToString();
                        selectedSVID = dataGridViewX1.Rows[selectedIndex].Cells[1].Value.ToString();

                        BLL.updateDiem(BLL.retrieveByIDAndSVID(selectedID, selectedSVID), lanNhapDiem, diem);
                        txtDiem.Enabled = false;
                    }
                    if (trangThai == "dang_them")
                    {
                        bdiem.DiemLan1 = diem;
                        if (BLL.insertDiem(bdiem))
                            MessageBox.Show("Nhập thành công", "Congratulation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        else
                            MessageBox.Show("Sai MSSV hoặc SV đã có điểm môn này", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtMSSV.ReadOnly = true;
                        txtDiem.ReadOnly = true;
                    }
                    dataGridViewX1.DataSource = BLL.returnListDiemByClass(IDMon, txtNamHoc.Text, txtHK.Text);

                    dataGridViewX1.Enabled = true;

                    HideContent();
                    // disable cac nut Luu va Huy
                    setBtnEnabled2(false);
                    setBtnEnabled1(true);

                    selectedInstance = BLL.retrieveByIndex(0);
                    updateTextboxes(selectedInstance);
                }
                else
                {
                    MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    btnHuy_Click(sender, e);
                }
            }
            
        }


        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai view va buttons
            selectedInstance = BLL.retrieveByIndex(0);
            updateTextboxes(selectedInstance);
            disableUpperTxt(true);

            dataGridViewX1.DataSource = BLL.returnListDiemByClass(cbbListMonHoc.SelectedValue.ToString(), txtNamHoc.Text, txtHK.Text);
            setBtnEnabled2(false);
            setBtnEnabled1(true);

            dataGridViewX1.Enabled = true;
            chkKhoaNhap.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMSSV.Text == "")
            {
                MessageBox.Show("Bạn phải chọn Bảng điểm cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa bảng điểm này không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    
                    
                    BLL.deleteDiem(selectedInstance);

                    dataGridViewX1.DataSource = BLL.returnListDiemByClass(cbbListMonHoc.SelectedValue.ToString(), txtNamHoc.Text, txtHK.Text);
                    selectedInstance = BLL.retrieveByIndex(0);
                    updateTextboxes(selectedInstance);
                }
            }
        }

        void updateTextboxes(BangDiem temp)
        {
            if (lanNhapDiem == 1)
            {
                txtMSSV.Text = temp.SVID;
                txtDiem.Text = temp.DiemLan1.ToString();
            }
            else if (lanNhapDiem == 2)
            {
                txtMSSV.Text = temp.SVID;
                txtDiem.Text = temp.DiemLan2.ToString();
            }
            else
            {
                txtMSSV.Text = temp.SVID;
                txtDiem.Text = temp.DiemLan3.ToString();
            }
        }

        private void clearTextboxes()
        {
            txtDiem.Text = "";
            txtMSSV.Text = "";
        }

        private void HideContent()
        {
            dataGridViewX1.Columns[9].Visible = false;
            dataGridViewX1.Columns[10].Visible = false;
            dataGridViewX1.Columns[11].Visible = false;
        }

        void setBtnEnabled1(bool val)
        {
            if (lanNhapDiem == 1)
                btnInsert.Enabled = val;
            else btnInsert.Enabled = false;
            btnXoa.Enabled = val;
            btnSua.Enabled = val;
        }

        void setBtnEnabled2(bool val)
        {
            btnSave.Enabled = val;
            btnHuy.Enabled = val;
        }

        private void disableLeftPanel(bool val)
        {
            txtHK.ReadOnly = val;
            txtNamHoc.ReadOnly = val;
            cbbListMonHoc.Enabled = !val;
        }



        private void disableUpperTxt(bool val)
        {
            txtMSSV.ReadOnly = val;
            txtDiem.ReadOnly = val;
        }


    }
}
